/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CreateChangeUser.java
 *
 * Created on 7-feb-2010, 19:30:32
 */
package View;

import Controller.Controller;
import Model.Rank;
import Model.User;
import Util.GenericTableModel;
import javax.swing.JOptionPane;

/**
 *
 * @author user
 */
public class CreateChangeUser extends javax.swing.JInternalFrame {

    User user = null;
    Rank defaultRank = Rank.USER;
    private UserView userview;

    /**
     * Creates new form CreateChangeUser
     */
    public CreateChangeUser(User user, UserView userview) {
        initComponents();
        this.user = user;
        this.userview = userview;
        Rank ranks[] = Rank.values();
        for (Rank r : ranks) {
            cmbBoxType.addItem(r);
        }
        cmbBoxType.setSelectedItem(defaultRank);

        if (user != null) {
            fillUser(user);
        }


    }

    private void fillUser(User user) {
        txtFieldUserName.setText(user.getUsername());
        cmbBoxType.setSelectedItem(user.getRank());
        jLabel1.setText("Change password");
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        txtFieldUserName = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        cmbBoxType = new javax.swing.JComboBox();
        txtFieldPassword = new javax.swing.JPasswordField();
        jLabel3 = new javax.swing.JLabel();
        btnSave = new javax.swing.JButton();
        btnClose = new javax.swing.JButton();
        lblError = new javax.swing.JLabel();

        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setResizable(true);
        setMinimumSize(new java.awt.Dimension(318, 317));
        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(flyaway.FlyAWayApp.class).getContext().getResourceMap(CreateChangeUser.class);
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        txtFieldUserName.setText(resourceMap.getString("txtFieldUserName.text")); // NOI18N
        txtFieldUserName.setName("txtFieldUserName"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        cmbBoxType.setName("cmbBoxType"); // NOI18N

        txtFieldPassword.setText(resourceMap.getString("txtFieldPassword.text")); // NOI18N
        txtFieldPassword.setName("txtFieldPassword"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        btnSave.setText(resourceMap.getString("btnSave.text")); // NOI18N
        btnSave.setName("btnSave"); // NOI18N
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        btnClose.setText(resourceMap.getString("btnClose.text")); // NOI18N
        btnClose.setName("btnClose"); // NOI18N
        btnClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCloseActionPerformed(evt);
            }
        });

        lblError.setText(resourceMap.getString("lblError.text")); // NOI18N
        lblError.setName("lblError"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(lblError, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 292, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtFieldUserName)
                            .addComponent(txtFieldPassword)
                            .addComponent(cmbBoxType, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnSave)
                                .addGap(18, 18, 18)
                                .addComponent(btnClose)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 12, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtFieldUserName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtFieldPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(cmbBoxType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSave)
                    .addComponent(btnClose))
                .addGap(18, 18, 18)
                .addComponent(lblError, javax.swing.GroupLayout.DEFAULT_SIZE, 24, Short.MAX_VALUE)
                .addContainerGap(120, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCloseActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnCloseActionPerformed

    private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
        String userName = txtFieldUserName.getText();
        char[] pw = txtFieldPassword.getPassword();
        String password = new String(pw);
        String errorMessage = "";

        if (userName.isEmpty()) {
            errorMessage = "*  Please enter a username <br>";
        }
        if ((user == null) && (password.isEmpty())) {
            errorMessage += "*  Please enter a password <br>";
        }

        userName.trim();
        password.trim();
        if ("".equals(errorMessage)) {
            if (user == null) {
                User u = new User();
                u.setUsername(userName);
                u.setPassword(password, true);
                u.setRank(defaultRank);

                if (Controller.Instance().save(u)) {
                    JOptionPane.showMessageDialog(this, "User \"" + u.getUsername() + "\" saved");
                    GenericTableModel<User> userModel = new GenericTableModel<User>(Controller.Instance().getUsers(true));
                    userview.getUsersTable().setModel(userModel);
                    GenericTableModel.removeColumn(userview.getUsersTable(), "password");
                    GenericTableModel.removeColumn(userview.getUsersTable(), "id");
                    this.dispose();
                } else {
                    lblError.setText("*  Username already exists");
                }
            } else {
                user.setUsername(userName);
                if (!password.isEmpty()) {
                    user.setPassword(password, true);
                }
                user.setRank((Rank) cmbBoxType.getSelectedItem());

                if (Controller.Instance().update(user)) {
                    JOptionPane.showMessageDialog(this, "User \"" + user.getUsername() + "\" saved");
                    GenericTableModel<User> userModel = new GenericTableModel<User>(Controller.Instance().getUsers(true));
                    userview.getUsersTable().setModel(userModel);
                    GenericTableModel.removeColumn(userview.getUsersTable(), "password");
                    GenericTableModel.removeColumn(userview.getUsersTable(), "id");
                    this.dispose();
                } else {
                    lblError.setText("*  Change User failed, username already exists");
                }
            }
        } else {
            lblError.setText("<html> " + errorMessage + " </html>");
        }
    }//GEN-LAST:event_btnSaveActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnClose;
    private javax.swing.JButton btnSave;
    private javax.swing.JComboBox cmbBoxType;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel lblError;
    private javax.swing.JPasswordField txtFieldPassword;
    private javax.swing.JTextField txtFieldUserName;
    // End of variables declaration//GEN-END:variables
}
